#include "tokenize.h"
#include "directory.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


int addChild(directoryNode_t* dir, directoryNode_t* child){
  dir->children[dir->numberOfChildren] = child;
  child->parent = dir;
  dir->numberOfChildren += 1;

}
int serializeDirectory(){
  directoryNode_t** children = root->children;
  //rootblock contains the block numbers of its childrens subdirectories
  int rootblock[BLOCKS];
  int i;
  for(i = 0; i < root->numberOfChildren; i++){
    rootblock[i] = children[i]->inodeNumber;
    writeDir(children[i]);
  }
}

#define OFFSET 2
#define CHARS_IN_INT 4
#define NUM_INTS_IN_NAME 5
void writeDir(directoryNode_t* dir){
  //block contains the serialized subdirectory tree
  int block[BLOCKS];
  block[0] = VALID;
  block[1] = dir->inodeNumber;
  char* filename = dir->name;
  char* name = filename;
  int i;
  for(i=0; i < NUM_INTS_IN_NAME; ++i){
    char chars[CHARS_IN_INT+1];
    chars[0] = name[0+4*i];
    chars[1] = name[1+4*i];
    chars[2] = name[2+4*i];
    chars[3] = name[3+4*i];
    chars[4] = '\0';
    block[i+OFFSET] = charstoint(chars);
    name +=4;
  }
}

void traverseDirectory(directoryNode_t* dir, int* block, int count){
  //visit action on node

  //end visit action
  directoryNode_t** children = dir->children;
  int i;
  for(i = 0; i < dir->numberOfChildren; i++){
    traverseDirectory(dir->children[i], block, count+1);
  }

}
int* stringtoints(char* string){
	int allocSize = 1024;
	int* returnArray = malloc(allocSize);
	int i;
	for(i = 0; i < strlen(string); i+=4){
		returnArray[i/4] = charstoint(string + i);
		if (i/4 > allocSize)
		{
			int* newArray = malloc((allocSize+1024));
			memcpy(newArray, returnArray, allocSize);
			allocSize += 1024;
			free(returnArray);
			returnArray = newArray;
		}
	}
	return returnArray;

}

char* intstostring(int* contents){
	int allocSize = 1024;
	int i;
	char* returnString = malloc(sizeof(char)*allocSize*4);
	for (i = 0; contents[i] != 0; i++)
	{
		returnString = strcat(returnString, inttochars(contents[i]));
		if (i > allocSize)
		{
			char* newString = malloc(sizeof(int)*(allocSize+1024)*4);
			memcpy(newString, returnString, allocSize);
			allocSize += 1024;
			free(returnString);
			returnString = newString;
		}
	}
	char* shortenedString = malloc(sizeof(char)*(strlen(returnString)+1));
	strcpy(shortenedString, returnString);
	free(returnString);
	return shortenedString;

}

int charstoint(char* chars){
char a = chars[3];
char b = chars[2];
char c = chars[1];
char d = chars[0];

int a1 = (int)a;
int b1 = (int)b;
int c1 = (int)c;
int d1 = (int)d;

a1 <<= 24;
b1 <<= 16;
c1 <<= 8;
return a1|b1|c1|d1;
}

char* inttochars(int place){
int d1 = place & 0x000000FF;
int c1 = place & 0x0000FF00;
int b1 = place & 0x00FF0000;
int a1 = place & 0xFF000000;
c1 >>= 8;
b1 >>= 16;
a1 >>= 24;
char a = (char) a1;
char b = (char) b1;
char c = (char) c1;
char d = (char) d1;
char* chars = malloc(sizeof(char)*(CHARS_IN_INT+1));
chars[0] = d;
chars[1] = c;
chars[2] = b;
chars[3] = a;
chars[4] = '\0';
return chars;
}

//input: path to a directory
//output: the pointer to the directory the path specifies
directoryNode_t* getDirectory(char* path){
  if(!strcmp(path, "/")) return root;
  char buffPath[BUFF_SIZE];
  strcpy(buffPath, path);
  char** paths = tokenize(buffPath, "/");
  directoryNode_t* dir = root;
  directoryNode_t* tempdir;
  int i;
  for(i = 0; paths[i] != NULL; ++i){
    dir = matchDirectory(dir, paths[i]);
    //if the file doesnt exist then dont return a directory
    if(dir == NULL) return NULL;

  }

  return dir;
}

directoryNode_t* matchDirectory(directoryNode_t* parent, char* childname){
  int i;
  directoryNode_t** children = parent->children;
  for(i = 0; i < parent->numberOfChildren; ++i){
    //if the file name matches then return it
    if(!strcmp(childname, (children[i])->name)){
      return children[i];
    }
  }
  //else, the file doesnt exist, so return null
  return NULL;
}
